package de.fu_berlin.minesweeper;


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class GameUI extends JPanel implements GameListener{
	private int COLS, ROWS;
	private float fProbability;
	private int intRecord, intCurrentPoints;
	
	private Field mainField;					// game field
	private GridBagConstraints objConstraints;	// GridBag constraints
	
	private JLabel lblStaticRem, lblRemaining;	// label indicating remaining fields
	private JLabel lblStaticRec, lblRec,
				   lblStaticPoints, lblPoints;	// labels for points
	private JButton btnStartNew;	// restart button
	
	private GameUI thisRef = this;	// self-reference needed in ActionListener
	
	public GameUI(){
		this(16,16,0.25f);
	}
	
	public GameUI(int iRow, int iCol, float fProb){
		this.ROWS = iRow;
		this.COLS = iCol;
		this.fProbability = fProb;
		this.mainField = new Field(iCol, iRow, fProb);
		this.intRecord = 0;
		this.intCurrentPoints = 0;
		
		// use GridBagLayout as LayoutManager
		this.setLayout(new GridBagLayout());
		this.objConstraints  = new GridBagConstraints();
		
		// add static label
		this.lblStaticRem = new JLabel("Remaining fields:");
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.ipady = 50;
		this.objConstraints.gridx = 0;
		this.objConstraints.gridy = 0;
		this.add(this.lblStaticRem,this.objConstraints);
		
		
		// add dynamic counter for remaining fields
		this.lblRemaining = new JLabel(Integer.toString(this.mainField.getRemainingFields()));
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.gridx = 1;
		this.objConstraints.gridy = 0;
		this.add(this.lblRemaining,this.objConstraints);
		
		// add field 
		mainField.addGameListener(this);
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.gridx = 0;
		this.objConstraints.gridy = 1;
		this.objConstraints.gridwidth = 2;
		this.add(mainField,this.objConstraints);
		
		// add restart button
		this.btnStartNew = new JButton("Restart");
		this.btnStartNew.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				// remove current component
				remove(mainField);
				
				
				// save and reset points
				if (intCurrentPoints > intRecord){
					intRecord = intCurrentPoints;
					lblRec.setText(Integer.toString(intRecord));
				}
				lblPoints.setText("0");
				intCurrentPoints = 0;
				
				// create new field
				mainField = new Field(ROWS, COLS, fProbability);
				mainField.addGameListener(thisRef);
				
				// set constraints
				objConstraints.fill = GridBagConstraints.HORIZONTAL;
				objConstraints.gridx = 0;
				objConstraints.gridy = 1;
				objConstraints.gridwidth = 2;
				
				// refresh remaining fields
				lblRemaining.setText(Integer.toString(mainField.getRemainingFields()));
				add(mainField, objConstraints);
			}
		});
		this.objConstraints.fill = GridBagConstraints.CENTER;
		this.objConstraints.gridx = 1;
		this.objConstraints.gridy = 0;
		this.objConstraints.gridwidth = 1;
		this.add(this.btnStartNew,this.objConstraints);
		
		// add static label
		this.lblStaticPoints = new JLabel("Gained Points:");
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.ipady = 30;
		this.objConstraints.gridx = 0;
		this.objConstraints.gridy = 2;
		this.add(this.lblStaticPoints,this.objConstraints);
				
		//add dynamic counter for the points
		this.lblPoints = new JLabel("0");
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.gridx = 1;
		this.objConstraints.gridy = 2;
		this.add(this.lblPoints,this.objConstraints);
		
		// add static label
		this.lblStaticRec = new JLabel("Your Record:");
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.ipady = 30;
		this.objConstraints.gridx = 0;
		this.objConstraints.gridy = 3;
		this.add(this.lblStaticRec,this.objConstraints);
						
		// label for the current record
		this.lblRec = new JLabel("0");
		this.objConstraints.fill = GridBagConstraints.HORIZONTAL;
		this.objConstraints.gridx = 1;
		this.objConstraints.gridy = 3;
		this.add(this.lblRec,this.objConstraints);
		
	}
	
	@Override
	public void gameWon(){
		JOptionPane.showMessageDialog(this, "Yeay! You won.");
	}
	
	@Override
	public void gameOver(){
		JOptionPane.showMessageDialog(this, "Awww. You lost.");
	}
	
	@Override
	public void remainingFieldsChanged(int intRemaining){
		this.lblRemaining.setText((intRemaining != -1) ? Integer.toString(intRemaining) : "0");
	}
	
	@Override
	public void gainedPoints(int newPoints){
		if (newPoints != -1){
			this.intCurrentPoints += newPoints;
			this.lblPoints.setText(Integer.toString(this.intCurrentPoints));	
		}
	}
}
